﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;

namespace BasicDataManagement
{
    /// <summary>
    /// AddSubItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddSubItemWindow : Window
    {
        public List<IngredientSimpleObject> ingredientList;

        public Ingredient IngredientResult { get; private set; }
        
        public AddSubItemWindow()
        {
            InitializeComponent();
            ingredientList = new List<IngredientSimpleObject>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ObservableCollection<IngredientSimpleObject> materialBindingList = new ObservableCollection<IngredientSimpleObject>(ingredientList);
            this.nameCombo.ItemsSource = materialBindingList;
        }

        private void nameCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            unitLabel.DataContext = ((sender as ComboBox).SelectedItem as IngredientSimpleObject);
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (nameCombo.Text == "" || dosageTB.Text == "")
            {
                MessageBox.Show("请填写完整信息！");
            }
            else
            {
                try
                {
                    IngredientResult = new Ingredient(nameCombo.Text, unitLabel.Content as string, Convert.ToDouble(dosageTB.Text), (nameCombo.SelectedItem as IngredientSimpleObject).Id);
                    DialogResult = true;
                    this.Close();
                }
                catch (FormatException)
                {
                    MessageBox.Show("请输入有效数字！");
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }
    }
}
